package com.qq.util;

public class StrUtil {

	public static long toLong(String s) {
		Long r = 0L;
		try {
			r = Long.parseLong(s);
		} catch (Exception e) {
		}
		return r;
	}

	public static int toInt(String s) {
		int r = 0;
		try {
			r = Integer.parseInt(s);
		} catch (Exception e) {
		}
		return r;
	}

	public static float toFloat(String s) {
		float r = 0;
		try {
			r = Float.parseFloat(s);
		} catch (Exception e) {
		}
		return r;
	}

	/**
	 * 判断字符串是否为数字
	 * @param str
	 * @return
	 */
	public static boolean isNumber(String str) {
		if (str == null) {
			return false;
		}
		int length = str.length();
		if (length == 0) {
			return false;
		}
		int i = 0;
		if (str.charAt(0) == '-') {
			if (length == 1) {
				return false;
			}
			i = 1;
		}
		for (; i < length; i++) {
			char c = str.charAt(i);
			if (c <= '/' || c >= ':') {
				return false;
			}
		}
		return true;
	}

	/**
	 * 数值取余，字符串取【长度和值和】的余
	 *
	 * @param key
	 * @param mod
	 * @return
	 */
	public static int getHash(String key, int mod) {
		int r = 0;
		if (isNumber(key)) {
			r = (int) (toLong(key) % mod);
		} else {
			long hash = key.length();
			for (int i = 0; i < key.length(); i++) {
				hash += key.charAt(i);
			}
			r = (int) (hash % mod);
		}

		return r;
	}

	/**
	 * 判断参数是否为空或者null
	 * @param src
	 * @return
	 */
	public static boolean isNullOrEmpty(String src){
		if(src != null && !"".equals(src)){
			return  false;
		}
		return  true;
	}
}
